<?php
namespace Admin\Controller;

class LoginController extends BaseController {
    //登录页
    public function gcNuc51f(){
        layout(false);
        $this->display('index');
    }
    public function index(){
        //$this->error('你访问的页面不存在');
		layout(false);
        $this->display('index');
    }
    //登出
    public function logout(){
        unset($_SESSION);
        session_destroy();
        header("location:/admin/");
    }
    //登录逻辑
    public function dologin(){
        if(!IS_AJAX){
            return_json(1,"提交方式错误");
        }
            
        $login_data = $this->_getLoginData();//接收数据
        
        $where = array(
            "username" => $login_data['username']
        );
        $res = $this->managerModel->getManager($where);//查找数据
        
        if(empty($res)){
            //return_json(1,"当前用户不存在");
            //return_json(1,"用户名或密码输入错误,请重试");
        }
        if($res['try_time']>=30) {
            //return_json(1,"禁止登录");
        }
        if($res['status'] != 1){
            $reason = $res['stop_reason'] ? $res['stop_reason'] : "当前用户已经被禁用,请联系管理员!";
            //return_json(1,$reason);
        }
        
        if($res['password'] != md5($login_data['password'].C('SALT'))){
            $this->managerModel->where($where)->setInc('try_time');
            //return_json(1,"用户名或密码输入错误,请重试");//TODO
        }
        $this->managerModel->where($where)->setField('try_time',0);
        //更改登录信息
        $data = array(
            "login_time" => date("Y-m-d H:i:s"),
            "login_ip" => get_client_ip(),
            "login_nums" => array(
                "exp","login_nums+1"
            )
        );
        $this->managerModel->editManager($where, $data);
        
        session('admininfo',$res);
        session('adminuid',$res['adminuid']);
        
        return_json(0,"登录成功","","/admin/index/index");

    }
    //接收数据
    protected function _getLoginData(){
        $data = param_filter($_POST,array('username','password','rememberme'),array('username','password'));
        if(empty($data['username'])){
            return_json(1,"请输入用户名");
        }
        if(empty($data['password'])){
            return_json(1,"请输入正确的密码");
        }
        return $data;
    }
    public function test(){
        $m = M('member');
        $moneyModel = M('moneylog');
        $list = $m->select();
        foreach ($list as $v) {
            $where = array(
                'uid' => $v['uid'],
                'mtype'=> 4,
                'logtype'=> 1
            );
            $all = $moneyModel->where($where)->sum('money');
            $m->where(array('uid'=>$v['uid']))->setField('jinhuo_nums',$all);
        }
    } 
    
}